package com.situ.servlet;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/admin/captcha")
public class CaptchaServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //响应验证码
        //验证码对象
        SpecCaptcha captcha = new SpecCaptcha(130, 44, 4);
        //设置相应内容类型
        resp.setContentType("image/gif");
        //设置不保留缓存
        resp.setHeader("Pragma", "No-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
        //将验证码答案在session中保存
        //获取一个会话对象。会话域
        HttpSession session = req.getSession();

        session.setAttribute("captcha", captcha.text().toLowerCase());
//        req.getSession().setAttribute("captcha", captcha.text().toLowerCase());


        captcha.out(resp.getOutputStream());

    }
}
